/*
 * This is the action responsible for collecting thread parameters
 * and putting the thread into the database to be posted in a forum
 */

package airport.forum.action;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import airport.forum.pojo.Forum;
import airport.forum.pojo.Thread;
import airport.forum.dao.ForumDAO;

import com.opensymphony.xwork2.ActionSupport;
//new thread
public class NewThreadAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

        private static final long serialVersionUID = 1L;
        
        private long forumId;
        private Forum forum;
		private Thread thread;
        private String threadTitle;
        private String openingPost;
		private String tags;
        private String username;
        
        protected HttpServletResponse servletResponse;  
        @Override   
        public void setServletResponse(HttpServletResponse servletResponse) {   
                this.servletResponse = servletResponse;   
        }   
        
        protected HttpServletRequest servletRequest;   
        @Override   
        public void setServletRequest(HttpServletRequest servletRequest) {   
                this.servletRequest = servletRequest;   
        }
        
        public String execute() {
                Cookie[] cookies = servletRequest.getCookies();
                String cookieName = "AirportUser";
                
                if (cookies != null) {
                        for (int i = 0; i < cookies.length; i++) {
                                if (cookies[i].getName().equals(cookieName)) {
                                        username = cookies[i].getValue();
                                        break;
                                }
                        }
                }
				
				thread = new Thread();
				thread.setThreadTitle(this.threadTitle);
				thread.setOpeningPost(this.openingPost);
				thread.setUserName(this.username);
				thread.setPostCount((long)0);
				//add tags handling here if needed
				
				thread.setTags(this.tags);
				
				ForumDAO dao = new ForumDAO();
				dao.addThreadToForum(thread, (long)1);
				
                return "success";
        }
		
		public void validate() {
			//validate the length of thread title does not exceed 45
			if (threadTitle.length() > 65 || threadTitle.length() < 5){
				addFieldError("error",
						"Thread Title must be less that 65 characthers and more than 4 characters");
			}
			//the length of opening post >3 && < 1000
			if (openingPost.trim().length() < 3 || openingPost.trim().length() > 1000){
				addFieldError("error",
						"Opening post must be longer than 3 characters and shorter than 1000 characters");
				
			}
			//the tags should be words like apple, bear, or numbers 12345. And the total length of tags String should not exceeds 45
			if (tags.length() > 45)
			{
				addFieldError("error",
						"Tags shouldn't exceed 45 characters");
			}
			String expression = "[[\\w]*,\\s]*";
			Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(tags);
			if (!matcher.matches()) {
				addFieldError("postThread", "please enter your tags in a valid format such as \"cat, dog, animal\"");
			}
		}
		
		public long getForumId() {
			return this.forumId;
		}
		public void setForumId(long forumId) {
			this.forumId =  forumId;
		}
		
		public Forum getForum() {
			return this.forum;
		}
		public void setForum(Forum forum) {
			this.forum = forum;
		}
		
		public Thread getThread() {
			return this.thread;
		}
		public void setThread(Thread thread) {
			this.thread = thread;
		}
		
		public String getThreadTitle() {
			return this.threadTitle;
		}
		public void setThreadTitle(String threadTitle) {
			this.threadTitle = threadTitle;
		}
		
		public String getOpeningPost() {
			return this.openingPost;
		}
		public void setOpeningPost(String openingPost) {
			this.openingPost = openingPost;
		}
		
		public String getTags() {
			return this.tags;
		}
		public void setTags(String tags) {
			this.tags = tags;
		}
		
		public String getUsername() {
			return this.username;
		}
		public void setUsername(String username) {
			this.username = username;
		}

}
